<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Modules\Blog\Service;


use App\Models\Blog\Notice;
use App\Models\System\User;
use Illuminate\Http\Request;

class NoticeService
{

    use Service;

    /**
     * 获取公告
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function NoticeIndexService( $request )
    {
        $user   = $this->getCommon()->UserCommonService();
        $select = [ 'id', 'name', 'user_id', 'is_show', 'content', 'status', 'updated_at', ];
        switch ( $request->input('news') ) {
            case 'system':
                $where = function ( $query ) use ( $request ) {
                    $query->where('user_id', 0);
                    $query->where('status', 1);
                    $query->where('is_show', 1);
                    if ( $request->filled('name') ) $query->where('name', $request->input('name'));
                    if ( $request->filled('created_at') ) {
                        $time       = $request->input('created_at');
                        $start_time = $time[ 0 ];
                        $end_time   = $time[ 1 ];
                        $query->whereBetween('created_at', [ $start_time, $end_time ]);
                    }
                };
                $user  = User::FirstModel([ 'id' => $user->id ])->notice()->pluck('id')->toArray();
                break;
            default:
                $where = function ( $query ) use ( $request, $user ) {
                    $query->where('status', 1);
                    $query->where('user_id', $user->id);
                    if ( $request->filled('is_show') ) $query->where('is_show', $request->input('is_show'));
                    if ( $request->filled('name') ) $query->where('name', $request->input('name'));
                    if ( $request->filled('created_at') ) {
                        $time       = $request->input('created_at');
                        $start_time = $time[ 0 ];
                        $end_time   = $time[ 1 ];
                        $query->whereBetween('created_at', [ $start_time, $end_time ]);
                    }
                };
                break;
        }

        $notice = Notice::GetModel($where, [], $select, $request->input('limit'));
        if ( $request->input('news') == 'system' ) foreach ( $notice as &$value ) {
            $value->is_show = 1;
            if ( in_array($value->id, $user) ) $value->is_show = 2;
        }
        return data($notice);
    }


    /**
     * 新增公告
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function NoticeCreateService( $request )
    {
        $user      = $this->getCommon()->UserCommonService();
        $validator = $this->getBlogValidator()->BlogNoticeValidator($request);
        if ( $validator->first() ) return message($validator->first());
        $data[ 'name' ]    = $request->input('name');
        $data[ 'is_show' ] = $request->input('is_show') == TRUE ? 1 : 2;
        $data[ 'content' ] = $request->input('content');
        $data[ 'user_id' ] = $user->id;
        Notice::CreateModel($data);
        return data(NULL, $this->getErrorCode()::message(209));
    }


    /**
     * 修改公告状态
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function NoticeIsShowService( $request )
    {
        $user = $this->getCommon()->UserCommonService();
        switch ( $request->input('status') ) {
            case 'system':
                $user = $this->getCommon()->UserCommonService();
                User::FirstModel([ 'id' => $user->id ])->notice()->detach($request->input('id'));
                break;
            default:
                $where           = function ( $query ) use ( $request, $user ) {
                    $query->where('id', $request->input('id'));
                    $query->where('user_id', $user->id);
                };
                $notice          = Notice::FirstModel($where, [], [ 'id', 'user_id', 'is_show' ]);
                $notice->is_show = $request->input('is_show') == 1 ? 1 : 2;
                $notice->save();
                break;
        }
        return data(NULL, $this->getErrorCode()::message(203));
    }

    /**
     * 删除公告
     *
     * @param $id
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function NoticeDestroyService( $id )
    {
        $user  = $this->getCommon()->UserCommonService();
        $where = function ( $query ) use ( $id, $user ) {
            if ( ! is_array($id) ) $query->where('id', $id);
            else $query->whereIn('id', $id);
            $query->where('user_id', $user->id);
        };
        Notice::DeleteModel($where);
        return data(NULL, $this->getErrorCode()::message(204));
    }

    /**
     * 获取公告详情
     *
     * @param $id
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function NoticeShowService( $id )
    {
        $user   = $this->getCommon()->UserCommonService();
        $where  = function ( $query ) use ( $id, $user ) {
            $query->where('user_id', $user->id);
            $query->where('id', $id);
        };
        $select = [ 'id', 'name', 'user_id', 'is_show', 'content', 'status', 'updated_at', ];
        $notice = Notice::FirstModel($where, [], $select);
        return data($notice);
    }

    /**
     * 更新 公告
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function NoticeUpdateService( $request )
    {
        $user      = $this->getCommon()->UserCommonService();
        $validator = $this->getBlogValidator()->BlogNoticeValidator($request);
        if ( $validator->first() ) return message(NULL, $validator->first());
        $where             = function ( $query ) use ( $request, $user ) {
            $query->where('id', $request->input('id'));
            $query->where('user_id', $user->id);
        };
        $data[ 'name' ]    = $request->input('name');
        $data[ 'is_show' ] = $request->input('is_show') == TRUE ? 1 : 2;
        $data[ 'content' ] = $request->input('content');
        $data[ 'user_id' ] = $user->id;
        Notice::UpdateModel($where, $data);
        return data(NULL, $this->getErrorCode()::message(210));
    }
}
